package com.loklok.code.confuse.util;

import com.intellij.psi.PsiType;
import com.intellij.psi.impl.source.PsiClassReferenceType;

public class TypeWrapperChecker {

    /**
     * 判断返回类型是否是 ResultResponse<T> 类型
     */
    public static boolean isResultResponseType(PsiType returnType) {
        return isWrappedType(returnType, "com.example.ResultResponse");
    }

    /**
     * 判断是否是 Page<T> 类型
     */
    public static boolean isPageType(PsiType returnType) {
        return isWrappedType(returnType, "org.springframework.data.domain.Page");
    }

    /**
     * 判断是否是 Optional<T> 类型
     */
    public static boolean isOptionalType(PsiType returnType) {
        return isWrappedType(returnType, "java.util.Optional");
    }

    /**
     * 判断是否是任意包装类型
     */
    private static boolean isWrappedType(PsiType returnType, String wrapperFqn) {
        if (!(returnType instanceof PsiClassReferenceType)) {
            return false;
        }

        com.intellij.psi.PsiClass resolved = ((PsiClassReferenceType) returnType).resolve();
        return resolved != null && wrapperFqn.equals(resolved.getQualifiedName());
    }
}
